package com.zhangjingcheng.yunyixia.util;

import com.zhangjingcheng.yunyixia.em.SuffixType;

import java.util.HashMap;
import java.util.Map;

public class ContentTypeUtil {
    
    private static final Map<String, String> CONTENT_TYPE_MAP = new HashMap<>();
    
    static {
        // 视频文件
        CONTENT_TYPE_MAP.put("mp4", "video/mp4");
        CONTENT_TYPE_MAP.put("avi", "video/avi");
        CONTENT_TYPE_MAP.put("mkv", "video/x-matroska");
        CONTENT_TYPE_MAP.put("mov", "video/quicktime");
        CONTENT_TYPE_MAP.put("wmv", "video/x-ms-wmv");
        CONTENT_TYPE_MAP.put("flv", "video/x-flv");
        CONTENT_TYPE_MAP.put("rmvb", "application/vnd.rn-realmedia-vbr");
        CONTENT_TYPE_MAP.put("webm", "video/webm");
        CONTENT_TYPE_MAP.put("m4v", "video/x-m4v");
        CONTENT_TYPE_MAP.put("3gp", "video/3gpp");
        CONTENT_TYPE_MAP.put("asf", "video/x-ms-asf");
        CONTENT_TYPE_MAP.put("rm", "application/vnd.rn-realmedia");
        CONTENT_TYPE_MAP.put("mpg", "video/mpeg");
        CONTENT_TYPE_MAP.put("mpeg", "video/mpeg");
        CONTENT_TYPE_MAP.put("ts", "video/mp2t");
        CONTENT_TYPE_MAP.put("vob", "video/x-ms-vob");
        CONTENT_TYPE_MAP.put("f4v", "video/x-f4v");
        CONTENT_TYPE_MAP.put("mts", "model/vnd.mts");

        // 音频文件
        CONTENT_TYPE_MAP.put("mp3", "audio/mpeg");
        CONTENT_TYPE_MAP.put("wav", "audio/wav");
        CONTENT_TYPE_MAP.put("flac", "audio/flac");
        CONTENT_TYPE_MAP.put("aac", "audio/aac");
        CONTENT_TYPE_MAP.put("wma", "audio/x-ms-wma");
        CONTENT_TYPE_MAP.put("ogg", "audio/ogg");
        CONTENT_TYPE_MAP.put("ape", "audio/ape");
        CONTENT_TYPE_MAP.put("m4a", "audio/mp4");
        CONTENT_TYPE_MAP.put("mid", "audio/midi");
        CONTENT_TYPE_MAP.put("midi", "audio/midi");
        CONTENT_TYPE_MAP.put("amr", "audio/amr");
        CONTENT_TYPE_MAP.put("ac3", "audio/ac3");
        CONTENT_TYPE_MAP.put("dts", "audio/vnd.dts");
        CONTENT_TYPE_MAP.put("opus", "audio/opus");
        CONTENT_TYPE_MAP.put("caf", "audio/x-caf");
        CONTENT_TYPE_MAP.put("au", "audio/basic");
        CONTENT_TYPE_MAP.put("aiff", "audio/aiff");

        // 图片文件
        CONTENT_TYPE_MAP.put("jpg", "image/jpeg");
        CONTENT_TYPE_MAP.put("jpeg", "image/jpeg");
        CONTENT_TYPE_MAP.put("png", "image/png");
        CONTENT_TYPE_MAP.put("gif", "image/gif");
        CONTENT_TYPE_MAP.put("bmp", "image/bmp");
        CONTENT_TYPE_MAP.put("webp", "image/webp");
        CONTENT_TYPE_MAP.put("svg", "image/svg+xml");
        CONTENT_TYPE_MAP.put("tiff", "image/tiff");
        CONTENT_TYPE_MAP.put("heic", "image/heic");
        CONTENT_TYPE_MAP.put("ico", "image/x-icon");
        CONTENT_TYPE_MAP.put("psd", "image/vnd.adobe.photoshop");
        CONTENT_TYPE_MAP.put("ai", "application/postscript");
        CONTENT_TYPE_MAP.put("eps", "application/postscript");
        CONTENT_TYPE_MAP.put("raw", "image/x-raw");
        CONTENT_TYPE_MAP.put("cr2", "image/x-canon-cr2");
        CONTENT_TYPE_MAP.put("nef", "image/x-nikon-nef");
        CONTENT_TYPE_MAP.put("dng", "image/x-adobe-dng");
        CONTENT_TYPE_MAP.put("tga", "image/x-tga");
        CONTENT_TYPE_MAP.put("pcx", "image/x-pcx");

        // 文档文件
        CONTENT_TYPE_MAP.put("pdf", "application/pdf");
        CONTENT_TYPE_MAP.put("txt", "text/plain; charset=UTF-8");
        CONTENT_TYPE_MAP.put("html", "text/html; charset=UTF-8");
        CONTENT_TYPE_MAP.put("htm", "text/html; charset=UTF-8");
        CONTENT_TYPE_MAP.put("xml", "application/xml; charset=UTF-8");
        CONTENT_TYPE_MAP.put("json", "application/json; charset=UTF-8");
        CONTENT_TYPE_MAP.put("css", "text/css; charset=UTF-8");
        CONTENT_TYPE_MAP.put("js", "application/javascript; charset=UTF-8");
        CONTENT_TYPE_MAP.put("sql", "application/sql");
        CONTENT_TYPE_MAP.put("md", "text/markdown; charset=UTF-8");
        CONTENT_TYPE_MAP.put("log", "text/plain; charset=UTF-8");
        CONTENT_TYPE_MAP.put("rtf", "application/rtf");
        CONTENT_TYPE_MAP.put("odt", "application/vnd.oasis.opendocument.text");
        CONTENT_TYPE_MAP.put("ods", "application/vnd.oasis.opendocument.spreadsheet");
        CONTENT_TYPE_MAP.put("odp", "application/vnd.oasis.opendocument.presentation");
        CONTENT_TYPE_MAP.put("csv", "text/csv; charset=UTF-8");
        CONTENT_TYPE_MAP.put("ini", "text/plain; charset=UTF-8");
        CONTENT_TYPE_MAP.put("cfg", "text/plain; charset=UTF-8");
        CONTENT_TYPE_MAP.put("conf", "text/plain; charset=UTF-8");
        CONTENT_TYPE_MAP.put("properties", "text/plain; charset=UTF-8");
        CONTENT_TYPE_MAP.put("yaml", "application/yaml");
        CONTENT_TYPE_MAP.put("yml", "application/yaml");
        CONTENT_TYPE_MAP.put("toml", "application/toml");
        CONTENT_TYPE_MAP.put("tex", "application/x-tex");
        CONTENT_TYPE_MAP.put("wps", "application/wps-office.wps");
        CONTENT_TYPE_MAP.put("et", "application/wps-office.et");
        CONTENT_TYPE_MAP.put("dps", "application/wps-office.dps");

        // Office文档文件
        CONTENT_TYPE_MAP.put("doc", "application/msword");
        CONTENT_TYPE_MAP.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        CONTENT_TYPE_MAP.put("xls", "application/vnd.ms-excel");
        CONTENT_TYPE_MAP.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        CONTENT_TYPE_MAP.put("ppt", "application/vnd.ms-powerpoint");
        CONTENT_TYPE_MAP.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
        
        // 压缩文件
        CONTENT_TYPE_MAP.put("zip", "application/zip");
        CONTENT_TYPE_MAP.put("rar", "application/x-rar-compressed");
        CONTENT_TYPE_MAP.put("7z", "application/x-7z-compressed");
        CONTENT_TYPE_MAP.put("tar", "application/x-tar");
        CONTENT_TYPE_MAP.put("gz", "application/gzip");
        CONTENT_TYPE_MAP.put("bz2", "application/x-bzip2");
        CONTENT_TYPE_MAP.put("xz", "application/x-xz");
        CONTENT_TYPE_MAP.put("lzma", "application/x-lzma");
        CONTENT_TYPE_MAP.put("cab", "application/vnd.ms-cab-compressed");
        CONTENT_TYPE_MAP.put("ace", "application/x-ace-compressed");
        CONTENT_TYPE_MAP.put("arj", "application/x-arj");
        CONTENT_TYPE_MAP.put("lzh", "application/x-lzh");
        CONTENT_TYPE_MAP.put("z", "application/x-compress");
        CONTENT_TYPE_MAP.put("tgz", "application/gzip");
        CONTENT_TYPE_MAP.put("tbz2", "application/x-bzip2");
        CONTENT_TYPE_MAP.put("iso", "application/x-iso9660-image");
        CONTENT_TYPE_MAP.put("dmg", "application/x-apple-diskimage");
        CONTENT_TYPE_MAP.put("pkg", "application/octet-stream");
    }


    /**
     * 根据文件名获取MIME类型
     *
     * @param fileName
     * @return
     */
    public static String getContentType(String fileName) {
        // 获取文件后缀
        String suffix = StringUtil.getSuffix(fileName, SuffixType.NO_POINT.name());
        if (suffix.isEmpty()) {
            return "application/octet-stream";
        }
        return CONTENT_TYPE_MAP.getOrDefault(suffix.toLowerCase(), "application/octet-stream");
    }
}
